package com.biao.java8.starem_test;

import com.biao.java8.demo.pojo.Employee;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Test02_ListToMap {
    public static void main(String[] args) {
        // list 转 map
        List<Employee> listEmployee = new ArrayList<>();
        listEmployee.add(new Employee("abiu", 18, 8000d));
        listEmployee.add(new Employee("二狗", 24, 16000d));
        listEmployee.add(new Employee("黑狗哥", 27, 24000d));

        // 不使用lambda写法
        Map<String, Employee> employeeMap = listEmployee.stream().collect(Collectors.toMap(new Function<Employee, String>() {
            @Override
            public String apply(Employee employee) {    // 第一个参数是key
                return employee.getName();
            }
        }, new Function<Employee, Employee>() {     // 第二个参数是value
            @Override
            public Employee apply(Employee employee) {
                return employee;
            }
        }));
        System.out.println("employeeMap: --------------------------------");
        employeeMap.forEach(new BiConsumer<String, Employee>() {
            @Override
            public void accept(String s, Employee employee) {
                System.out.println(s + " " + employee);
            }
        });
        System.out.println();

        // 使用lambda写法
        Map<String, Employee> employeeMapLambda = listEmployee.stream().collect(Collectors.toMap(Employee::getName, employee -> employee));
        System.out.println("employeeMapLambda: --------------------------------");
        employeeMapLambda.forEach((s, employee) -> System.out.println(s + " " + employee));
    }
}
